package com.oocl.euc.core.requestform.controller;

import com.oocl.euc.common.constant.MailTemplateKey;
import com.oocl.euc.common.utils.JacksonUtil;
import com.oocl.euc.core.attachment.repository.AttachmentRepository;
import com.oocl.euc.core.builder.EmailTemplateBuilder;
import com.oocl.euc.core.builder.RequestFormBuilder;
import com.oocl.euc.core.builder.RequestRemarkBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.EmailTemplate;
import com.oocl.euc.core.maintenance.repository.EmailTemplateRepository;
import com.oocl.euc.core.requestform.repository.RequestFormRepository;
import com.oocl.euc.core.requestform.repository.RequestRemarksRepository;
import com.oocl.euc.core.requestform.vo.RequestRemarksVO;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


public class RequestRemarksControllerIT extends ITTestBase {


    @Autowired
    private RequestRemarkBuilder requestRemarkBuilder;


    @Autowired
    private EmailTemplateBuilder emailTemplateBuilder;


    @Autowired
    private RequestRemarksRepository requestRemarksRepository;

    @Autowired
    private EmailTemplateRepository emailTemplateRepository;

    @Autowired
    private AttachmentRepository attachmentRepository;

    @Autowired
    private RequestFormBuilder requestFormBuilder;

    @Autowired
    private RequestFormRepository requestFormRepository;


    @After
    public void reset() {

        requestRemarksRepository.deleteAll();
        emailTemplateRepository.deleteAll();
        attachmentRepository.deleteAll();
        requestFormRepository.deleteAll();


    }


    @Test
    public void should_return_new_created_request_remarks_when_given_request_remarks_vo() throws Exception {
        RequestRemarksVO requestRemarksVO = new RequestRemarksVO();
        requestRemarksVO.setSender("WUNE");
        requestRemarksVO.setReceiver("REQUESTER");
        requestRemarksVO.setRefNo("123456");
        requestRemarksVO.setDesc("desc");
        EmailTemplate emailTemplate = emailTemplateBuilder.defaultBuilder()
                .withKey(MailTemplateKey.CONVERSATION_EMAIL)
                .withContent("test")
                .saveToDB();
        requestFormBuilder.defaultBuilder().withRefNO("123456").withRequestDomainID("WUNE").withEmail("neal.wu@oocl.com").saveToDB();
        mockMvc.perform(post("/rest/requestRemarks/add")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(JacksonUtil.objToJson(requestRemarksVO)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.sender").value("WUNE"))
                .andExpect(jsonPath("$.refNo").value("123456"));
    }

    @Test
    public void should_return_conversationvo_list_when_given_ref_no() throws Exception {
        requestRemarkBuilder.defaultBuilder().withRefNo("123456").withDescription("desc").saveToDB();
        mockMvc.perform(get("/rest/requestRemarks/refNo/123456"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.size()").value(1))
                .andExpect(jsonPath("$[0].description").value("desc"));
    }


}
